﻿using Microsoft.Extensions.Caching.Memory;

namespace Common.Cache;

public class MemoryCacheManager : ICacheManager
{
    private readonly IMemoryCache _memoryCache;

    public static readonly string CACHE_NAME = "MemoryCache";


    public MemoryCacheManager(IMemoryCache memoryCache)
    {
        _memoryCache = memoryCache;
    }

    public string GetCacheName()
    {
        return CACHE_NAME;
    }

    public void Set(string key, object value)
    {
        _memoryCache.Set(key, value);
        // _memoryCache.GetOrCreate()
    }

    public void Set(string key, object value, int expiredSeconds)
    {
        if (expiredSeconds > 0)
        {
            _memoryCache.Set(key, value, TimeSpan.FromSeconds(expiredSeconds));
        }
        else
        {
            Set(key, value);
        }
    }

    public object? Get(string key)
    {
        int val = 1;
        int? nullableValue = null;
        var val2 = nullableValue ?? 0;

        return _memoryCache.Get(key);
    }

    public T Get<T>(string key)
    {
        return _memoryCache.Get<T>(key);
    }
}